<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="insSampleCode">
              <el-input clearable v-model="filters.insSampleCode" placeholder="样本编号"></el-input>
            </el-form-item>
            <el-form-item prop="institutionId">
              <el-select clearable v-model="filters.institutionId" :clearable="true" filterable placeholder="请选择医检机构">
                <el-option v-for="item in pickInstitutionList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="insSampleName">
              <el-input clearable v-model="filters.insSampleName" placeholder="样本名称"></el-input>
            </el-form-item>
            <!--<el-form-item prop="appointState">
              <el-select v-model="filters.appointState" :clearable="true" placeholder="请选择报告单状态">
                <el-option
                  v-for="item in reviewStatue"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="date">
              <el-date-picker v-model="filters.date" :clearable="true" type="datetimerange" range-separator="至" start-placeholder="送检开始日期" end-placeholder="送检结束日期"></el-date-picker>
            </el-form-item>-->
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="listSpecimen">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap" style="justify-content: start">
          <el-tooltip content="刷新" placement="top">
            <el-button style="margin-left: auto" circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionExport" @click="handleClickExport()" size="mini"> 表格导入</el-button>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="specimenList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="insSampleCode" align="center" label="样本编号" width="70"></el-table-column>
        <el-table-column prop="insSampleName" align="center" label="样本名称" width="200"></el-table-column>
        <el-table-column prop="institutionName" label="医检名称" width="200"></el-table-column>
        <!-- <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag
                size="mini"
                :type="formatStateType(scope.row.state)"
                effect="dark"
              >
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center" width="auto">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" icon="el-icon-view" size="mini">查看</el-button>
            <el-button v-has="permissionEdit" @click="handleClickEditor(scope.row)" icon="el-icon-edit-outline" size="mini">编辑</el-button>
            <el-button v-has="permissionUpStatus" @click="handleIsDisable(scope.row)" icon="el-icon-delete" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <uploadFile ref="uploadFile" @uploadFileList="getUploadFileList" :accept="uploadAccept" folder="medicalSpecimen" :importTemplateUrl="importTemplateUrl"> </uploadFile>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import medicalApi from '@/api/medicalspecimen/index';
import InstitutionApi from '@/api/institution/institution';
import UploadRecordApi from '@/api/system/uploadRecord';
import { MEDICAL_SPECIMEN_ITEM_IMPORT_TEMPLATE } from '@/constants/constants.js';
import uploadFile from '@/components/UploadFile';

export default {
  name: 'MedicalSpecimenManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      specimenList: [],
      importTemplateUrl: '', //导入模板的地址
      uploadAccept: 'all', // 上传的文件类型 0.all 1.pdf 2.img 3.video 4.excel
      filters: {
        insSampleCode: '',
        institutionId: '',
        insSampleName: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      pickInstitutionList: [],
      // reviewStatue: [
      //   { label: "检测中", value: 0 },
      //   { label: "已出结果", value: 1 },
      // ],
      // agenciesStatue: [
      //   { label: "未打印", value: 0 },
      //   { label: "已打印", value: 1 },
      // ],

      permissionAdd: 'SYSTEM_MANAGER_MEDICALSPECIMEN_ITEM_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_MEDICALSPECIMEN_ITEM_LIST_SEE',
      permissionExport: 'SYSTEM_MANAGER_MEDICALSPECIMEN_ITEM_LIST_EXPORT',
      permissionEdit: 'SYSTEM_MANAGER_MEDICALSPECIMEN_ITEM_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_MEDICALSPECIMEN_ITEM_LIST_DELETE',
      excelData: {},
      allList: [],
      uploadData: {
        uploadType: 5, // 医检
        fileDesc: '',
        uploadFile: '',
      },
    };
  },
  components: {
    uploadFile,
  },
  created() {
    window.addEventListener('keydown', this.keyDown);
  },
  mounted() {
    this.init();
  },
  activated() {
    this.listSpecimen();
    // if (this.$route.meta.keepAlive) {
    // }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    handleClickExport() {
      this.uploadAccept = 'excel';
      this.$refs.uploadFile.handleCreate();
    },
    // excel导入
    getUploadFileList(val) {
      let _this = this;
      if (!val) {
        _this.$Utils.messageError('请选择文件上传');
        return;
      }
      _this.uploadData.uploadFile = val[0];
      _this.uploadData.fileDesc = 'EXCEL导入医检标本信息';
      medicalApi
        .importData({
          uploadUrl: _this.uploadData.uploadFile,
        })
        .then((res) => {
          _this.$Utils.notifySuccess();
          _this.listSpecimen();
        })
        .catch((err) => {
          console.log(err, ' InstitutionItemApi.importItemExcel');
        });
    },
    init() {
      let _this = this;
      _this.importTemplateUrl = MEDICAL_SPECIMEN_ITEM_IMPORT_TEMPLATE; //获取导入模板的地址
      // _this.initDate();
      _this.listInstitution();
      _this.listSpecimen();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.listSpecimen();
      }
    },


    // initDate() {
    //   let begin = new Date();
    //   begin.setHours(0);
    //   begin.setMinutes(0);
    //   begin.setSeconds(0);
    //   begin.setMonth(begin.getMonth() - 1);
    //   let end = new Date();
    //   end.setHours(23);
    //   end.setMinutes(59);
    //   end.setSeconds(59);
    //   this.filters.date = [begin, end];
    // },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.listSpecimen();
    },
    // 当过滤器放在第三方组件下 若 row, column, 会报错
    // formatState(cellValue) {
    //   if (cellValue == -1) {
    //     return "删除";
    //   } else if (cellValue == 0) {
    //     return "禁用";
    //   } else if (cellValue == 1) {
    //     return "启用";
    //   } else if (cellValue == 2) {
    //     return "待审核";
    //   }
    // },
    // formatStateType(cellValue) {
    //   if (cellValue == -1) {
    //     return "danger";
    //   } else if (cellValue == 0) {
    //     return "info";
    //   } else if (cellValue == 1) {
    //     return "success";
    //   } else if (cellValue == 2) {
    //     return "";
    //   }
    // },
    formatDate(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, FORMAT_DATE_YYYY_MM_DD);
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    listInstitution() {
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        institutionType: 1,
      };
      InstitutionApi.getInstitutionNameList(cond)
        .then((res) => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.pickInstitutionList = res.data.map((item) => {
              return {
                value: item.institutionId,
                label: item.institutionName,
              };
            });
          }, 200);
        })
        .catch((error) => {
          _this.pickInstitutionList = [];
          console.error(error);
        });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listSpecimen();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listSpecimen();
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/item/medicalSpecimenEdit',
        query: { id: row.insSampleId },
      });
    },
    handleIsDisable(row) {
      let _this = this;
      this.$confirm('是否删除该医检标本', message.TITLE_PROMPT, {}).then(() => {
        medicalApi
          .delSpecimen({
            insSampleId: row.insSampleId,
            state: -1,
          })
          .then((res) => {
            _this.listSpecimen();
            _this.$message({
              type: 'success',
              message: '操作成功',
            });
          });
      });
    },

    handleClickAdd() {
      let _this = this;
      _this.$router.push('/item/medicalSpecimenAdd');
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/item/medicalSpecimenDetail',
        query: { id: row.insSampleId },
      });
    },
    listSpecimen() {
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        insSampleCode: _this.filters.insSampleCode,
        institutionId: _this.filters.institutionId,
        insSampleName: _this.filters.insSampleName,
      };
      medicalApi.getList(cond).then((res) => {
        _this.specimenList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
